package cn.htsyb.synergy.orm.jpa.criterion;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;

/**
 * 条件表达式
 *
 * @author lakeside hubin_yuan.410@hotmail.com
 * @version V1.0.0
 * @date 2022/5/11
 * @since 1.0
 */
@Setter
@Getter
@NoArgsConstructor
@Schema(description = "查询条件表达式")
public class QueryExpress implements Serializable {
    private String field;
    private Operator operator = Operator.EQ;
    private Object value;

    public QueryExpress(String field, Operator operator, Object value) {
        setField(field);
        setOperator(operator);
        setValue(value);

    }

    public void setField(String field) {
        String connector = "|";
        this.field = field;
        // 增加一个语法糖 (示例：field|gt=2)
        if (operator == null && field != null && field.contains(connector)) {
            String[] splits = field.split(connector);
            this.field = splits[0];
            this.operator = Operator.valueOf(splits[1]);
        }
    }

    public void setOperator(Operator operator) {
        this.operator = operator;
    }

    public void setValue(Object value) {
        this.value = value;
        // 增加一个语法糖(示例：field="is_null")
        if (value instanceof String) {
            if ("IS_NULL".equalsIgnoreCase(String.valueOf(value))) {
                this.operator = Operator.IS_NULL;
            } else if ("NOT_NULL".equalsIgnoreCase(String.valueOf(value))) {
                this.operator = Operator.NOT_NULL;
            }
        }
    }

    public enum Operator implements Serializable {
        EQ("=", "等于"),
        NEQ("<>", "不等于"),
        LIKE("like", "全模糊查询"),
        LIKE_C("like", "自定义模糊查询"),
        GT(">", "大于"),
        LT("<", "小于"),
        GTE(">=", "大于等于"),
        LTE("<=", "小于等于"),
        IN(" in", "在列表中"),
        NOT_IN("not in", "不在列表中"),
        IS_NULL("is null", "为空"),
        NOT_NULL("is not null", "不为空"),
        @Deprecated
        IS_MEMBER("??", "??"),
        @Deprecated
        NOT_MEMBER("??", "??");


        private final String value;
        private final String label;


        Operator(String value, String label) {
            this.value = value;
            this.label = label;
        }

        public String getLabel() {
            return label;
        }

        public String getValue() {
            return this.value;
        }

    }

}


